package zgs;

import java.lang.reflect.Method;
import java.util.Scanner;

public class ReflectDemo4 {
    public static void main(String[] args) throws Exception {
        Person p = new Person();
        p.sayHello();

        //实例化和new一个Person类的对象一样
       /* Class cls = Class.forName("zgs.Person");
        Object obj = cls.newInstance();
        Method m = cls.getMethod("sayHello");
        m.invoke(obj);*/

        //通过类对象获取方法对象(通过用户输出使代码更加灵活多变)
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入一个类名:");
        String classname = scan.nextLine();
        System.out.println("请输入一个方法名:");
        String methodName = scan.nextLine();

        Class cla = Class.forName(classname);
        Object obj = cla.newInstance();
        Method method = cla.getMethod(methodName);
        method.invoke(obj);










    }
}
